var commodityMod = $(".commodityClass").first().clone();
var jizhuanLiMod = $(".jizhuan-list li").first().clone();

$(".clearfixUl").html("");
$(".jizhuan-list").html("");
commodityMod.show();
jizhuanLiMod.show();

//启动遍历商品
showThreeList(1);
jizhuan();

//查询分类
$.get('/classify/list', {}, function (data, status, xhr) {

    $("#classifyListId").html("");
    data.forEach(function (classify) {
        var li = "<li><a href='good/showp?id=";
        li += classify.classificationId;
        li += "'>" + classify.classificationName;
        li += "<i class='icon'>&#xe600;</i></a></li>";
        $("#classifyListId").append(li)
    })
});

/*分类顶部固定*/
$(document).scroll(function () {
    if ($(document).scrollTop() > 140) {

        $('.category').css({
            'position': 'fixed',
            'top': '20px'
        })

        $('.main-box').css('margin-left', '190.8px')
    } else {
        $('.category').removeAttr('style')
        $('.main-box').css('margin-left', '0px')
    }
})

/*分类特效*/
$('.category ul li a').mouseover(function () {
    $(this).find('i').show()
    $(this).find('i').animate({
        display: 'inline-block',
        right: '5px'
    }, 300)
}).mouseout(function () {
    $(this).find('i').stop()
    $(this).find('i').css({
        display: 'none',
        right: '80px'
    })

})


//显示推荐
function showThreeList(pageNum) {
    $(".list-header a").removeClass("active");
    $("#recommend").addClass("active");
    $.get(
        '/good/showThreeList',
        {'pageNum': pageNum},
        function (data, status, xhr) {
            clearfix(data, "showThreeList");
        }
    );
}

//显示最新
function showNewestList(pageNum) {
    $(".list-header a").removeClass("active");
    $("#new").addClass("active");
    $.get(
        '/good/showNewestList',
        {'pageNum': pageNum},
        function (data, status, xhr) {
            clearfix(data, "showNewestList");
        }
    );
}

function clearfix(data, method) {
    $(".clearfixUl").html("");
    data.list.forEach(function (commodity) {
        var li = commodityMod.clone();
        li.find("a").prop("href", "good/showp/" + commodity.commodityId);
        if (commodity.commodityImgList.length) {
            li.find("img").prop("src", "/images/goods/" + commodity.commodityImgList[0].imgUrl);
        }
        li.find(".good-title").text(commodity.commodityName);
        li.find(".good-price").text("￥" + commodity.nowPrice);
        li.find(".pub-time").text("发布于: " + commodity.releaseDate);
        $(".clearfixUl").append(li)

        //分页
        $("#page-div").html("");
        var a = "<a href='#' onclick='" + method + "(1)'>首页</a>"
        $("#page-div").append("<a href='#' onclick='" + method + "(1)'>首页</a>");
        //<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>1</em></span>

        data.navigatepageNums.forEach(function (i) {
            if (i == data.pageNum) {
                $("#page-div").append("<span class='layui-laypage-curr'><em class='layui-laypage-em'></em><em>" + i + "</em></span>")
            } else {
                $("#page-div").append("<a href='#' onclick='" + method + "(" + i + ")'>" + i + "</a>");
            }
        })

        $("#page-div").append("<a href='#' onclick='" + method + "(" + data.navigateLastPage + ")'>尾页</a>");


    })
}


//显示推荐
function jizhuan() {
    $.get(
        '/good/jizhuanList',
        {},
        function (data, status, xhr) {
            $(".jizhuan-list").html("");
            data.list.forEach(function (commodity) {
                var li = jizhuanLiMod.clone();
                li.find("a").prop("href", "good/showp/" + commodity.commodityId);
                if (commodity.commodityImgList.length) {
                    li.find("img").prop("src", "/images/goods/" + commodity.commodityImgList[0].imgUrl);
                }
                li.find(".jizhuan-title").text(commodity.commodityName);
                li.find(".yhq").text("￥" + commodity.nowPrice);
                li.find(".jizhuan-price").text("￥" + commodity.costPrice);
                $(".jizhuan-list").append(li)
            })
        })
}










